-- @owner: @li-xin12345
-- @date: 2023/7/31
-- @testpoint: 普通非压缩表，修改compresstype=1,compress_chunk_size为512/1024及compress_prealloc_chunks，部分步骤合理报错
-- @history: Modified by li-xin12345 2025.2.12：研发代码变更，取消参数控制，适配用例

--step1:建普通表并插入数据;expect:成功
drop table if exists t_alter_rowcompress_0005;
create table t_alter_rowcompress_0005(id int, c1 text);
insert into t_alter_rowcompress_0005 select generate_series(1, 1000), 'testrowcompress';

drop function if exists compress_func_findpath(character varying);
create or replace function compress_func_findpath(character varying)
returns character varying
language plpgsql
as
$body$
declare
relpath character varying;
begin
relpath = (select pg_relation_filepath(relname::regclass) from pg_class where relname = $1);
return relpath;
end;
$body$;
/

--step2:修改压缩参数compresstype=1,compress_chunk_size 512,compress_prealloc_chunks为0到7;expect:成功
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=0);
checkpoint;
select is_compress, file_count, logic_size, physic_size, compress_ratio from pg_catalog.compress_ratio_info(compress_func_findpath('t_alter_rowcompress_0005'));

alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=1);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=2);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=3);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=4);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=5);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=6);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=7);
checkpoint;

select is_compress, file_count, logic_size, physic_size, compress_ratio from pg_catalog.compress_ratio_info(compress_func_findpath('t_alter_rowcompress_0005'));
select chunk_size, algorithm from pg_catalog.compress_address_header('t_alter_rowcompress_0005', 0);

--step3:修改压缩参数compresstype=1,compress_chunk_size 1024,compress_prealloc_chunks为0到7;expect:成功
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=0);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=1);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=2);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=3);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=4);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=5);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=6);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=7);
checkpoint;

select is_compress, file_count, logic_size, physic_size, compress_ratio from pg_catalog.compress_ratio_info(compress_func_findpath('t_alter_rowcompress_0005'));
select chunk_size, algorithm from pg_catalog.compress_address_header('t_alter_rowcompress_0005', 0);

--step4:修改压缩参数compresstype=1,compress_chunk_size 512/1024,compress_prealloc_chunks取值范围外;expect:合理报错
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=-1);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=8);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=-1);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=8);

--step5:修改压缩参数compresstype=1,compress_chunk_size 512/1024,compress_prealloc_chunks无效值;expect:合理报错
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks='test');
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=1024,compress_prealloc_chunks=2.2);
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks='test');
alter table t_alter_rowcompress_0005 set (compresstype=1,compress_chunk_size=512,compress_prealloc_chunks=2.2);

--step6:清理环境;expect:成功
drop function if exists compress_func_findpath(character varying);
drop table if exists t_alter_rowcompress_0005;